<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>游戏交易方式维护</title>
	<%@ include file="/view/common/tags.jsp"%>
	<script type="text/javascript" src="${sysManagerV1}/js/My97DatePicker/WdatePicker.js"> </script>
    <style type="text/css">
    table tbody tr {text-align: center;}
    .inputW{width: 80px;}
    </style>
</head>
<body>
	<section class="content">
        <form method="post" cssClass="form-horizontal" action="" id="saveAndUpdateFrom" target="_parent">
            <input type="hidden" id="token" name="token" value=""/>
            <input type="hidden" id="pageData" name="pageData" value=""/>
        </form>
        <form method="post" cssClass="form-horizontal" action="" id="dataFrom" target="_parent">
    	<div class="row">
            <div class="col-md-12">
                <div class="box">
                	<div class="box-body">
						<div class="panel panel-default">
                            <div class="panel-body" style="padding-bottom:5px;">
                                <div style="font-weight: bold;">
                                    <label>游戏名称：</label>
                                    <span id="gameNameSpan"><%=request.getParameter("gameName")%></span>
                                    <input type="hidden" id="gameId" name="" value="<%=request.getParameter("gameId")%>"/>
                                    <input type="hidden" id="gameName" name="" value="<%=request.getParameter("gameName")%>"/>
                                </div> 
                        	</div>
						</div>
                        <table class="table table-hover table-condensed table-bordered" id="dataTable" >
                            <thead>
                            	<tr>
                                	<th data-field="index" data-align="center" data-sortable="false" data-formatter="indexFormatter" >序号</th>
                                	<th data-field="operatorName" data-align="center" data-sortable="true">交易方式</th>
                                	<th data-field="gameName" data-align="center" data-sortable="true">一般交易排序</th>
                                	<th data-field="gameDes" data-align="center" data-sortable="true">一般交易</th>
                                    <th data-field="orderNum" data-align="center" data-sortable="true">默认交易方式</th>
                                    <!-- <th data-field="optTime" data-align="center" data-sortable="true">收货交易排序</th>
                                    <th data-field="optAccount" data-align="center" data-sortable="true">收货交易</th> -->
                                    <th data-field="useFlag" data-align="center" data-sortable="true">交易地点</th>
                                </tr>
                            </thead>
                            <tbody id="tbody">
                                    <tr data-index='0'>
                                        <td colspan="7">正在获取数据。。。</td>
                                    </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
       	</div>
        </form>
	</section>
   <%@ include file="/view/common/footer.jsp"%>
</body>
<script>
	// 提示信息
	var msgg='<%=request.getAttribute("msg")%>';
	if (msgg && msgg!=null && msgg!='null') { 
		  layer.msg(msgg);
	}
	var tableIndex = 0; // 添加修改的弹出框的ID
	var exchangeWaySize = ""; // 交易方式数量
	
	// 加载页面数据
    $(function(){
        $("#token").val(sessionStorage.getItem("token"));
        
        $.ajax({
            url: "${tradeV1}/tradeBaseGameVsExchangeWay/gameVsExchangeWayPageData",
            type: "post",
            cache: false,
            data: {"gameId":$("#gameId").val(), token: sessionStorage.getItem("token")},
            dataType:"json",
            async:false,
            success: function(data) {
            	exchangeWaySize = data.exchangeWayAll.length;
                initPageData(data.exchangeWayAll, data.exchangeWay, data.exchangeAddress);
            }
        });
    })
	
	function initPageData(exchangeWayAll, exchangeWay, exchangeAddress){
		var html = "";
		var gameId = $("#gameId").val();
		var gameName = $("#gameName").val();
		
	    var orderNumTD = "<td></td>";
		var useWayTD = "<td></td>";
	    var isDefaultTD = "<td></td>";
	    var exchangeAddressTD = "<td></td>";
	    
		var index = 0;
		for(var i in exchangeWayAll){
			index = Number(i) + 1;
			orderNumTD = "<td id='orderNumTD" + index + "'></td>";
			useWayTD = "<td><input type='checkbox' id='useWay' name='useWay' value='' index='" + index + "' exchangeWay='" + exchangeWayAll[i].value + "' onclick='useWayBtn(this, " + index + ")'/></td>";
			isDefaultTD = "<td><input type='checkbox' id='isDefault' name='isDefault' value='0' onclick='isDefaultBtn(this)'/></td>";
			exchangeAddressTD = "<td><input type='text' class='form-control' id='exchangeAddress" + index + "' name='exchangeAddress' value=''/></td>";
			
			html += "<tr data-index='" + index + "'>";
			html += "<td>" + index + "</td>";
			html += "<td><span id='wayNameSpan'>" + exchangeWayAll[i].label + "</span>";
			html += "    <input type='hidden' id='gameId' name='gameId' value='" + gameId + "'/>";
			html += "    <input type='hidden' id='gameName' name='gameName' value='" + gameName + "'/>";
			html += "    <input type='hidden' id='wayId' name='wayId' value='" + exchangeWayAll[i].value + "'/>";
			html += "    <input type='hidden' id='wayName' name='wayName' value='" + exchangeWayAll[i].label + "'/>";
			html += "</td>";
			for(var a in exchangeWay){
				if(exchangeWayAll[i].value == exchangeWay[a].wayId){
					orderNumTD = "<td><input type='text' class='form-control inputW' id='orderNum" + index + "' name='orderNum' value='" + exchangeWay[a].orderNum + "'/></td>";
    				if(exchangeWay[a].useWay == 10){
        				useWayTD = "<td><input type='checkbox' id='useWay' name='useWay' value='10' index='" + index + "' exchangeWay='" + exchangeWayAll[i].value + "' checked='checked' onclick='useWayBtn(this, " + index + ")'/></td>"			
    				}
    				if(exchangeWay[a].isDefault == 1){
    					isDefaultTD = "<td><input type='checkbox' id='isDefault' name='isDefault' value='1' checked='checked' onclick='isDefaultBtn(this)'/></td>";
    				}
				}
			}
			for(var b in exchangeAddress){
				if(exchangeWayAll[i].value == exchangeAddress[b].wayId && exchangeAddress[b].gameId == gameId
						&& exchangeWayAll[i].value != 20){
					exchangeAddressTD = "<td><input type='text' class='form-control' id='exchangeAddress" + index + "' name='exchangeAddress' value='" + exchangeAddress[b].exchangeAddress + "'/></td>";
				}
				if(exchangeWayAll[i].value == 20){
					exchangeAddressTD = "<td><input type='hidden' name='exchangeAddress' value=''/></td>";
				}
			}
			
			html += orderNumTD;
			html += useWayTD;
			html += isDefaultTD;
			/* html += "<td></td>";
			html += "<td></td>"; */
			html += exchangeAddressTD;
			html += "</tr>";
		}
		//$("#tbody").html("<form method='post' action='' id='saveAndUpdateFrom'>" + html + "</form>");
		$("#tbody").html(html);
	}
	
	// 启用/停用 
    function useWayBtn(obj, i) {
		if(obj.checked){
			$(obj).val("10");
            $("#orderNumTD" + i).html("<td><input type='text' class='form-control inputW' id='orderNum" + i + "' name='orderNum' value=''/></td>");
		} else {
			$(obj).val("");
            $("#orderNumTD" + i).html("");
		}
    }
	
	// 是否默认
    function isDefaultBtn(obj) {
		if(obj.checked){
			$("input[name='isDefault']").prop("checked", false);
			$("input[name='isDefault']").val("0");
			$(obj).prop("checked", true);
			$(obj).val("1");
		} else {
			$(obj).val("0");
		}
    }

    // 行点击事件
    $(function(){
         $("body").on("click","table>tbody>tr",function(){
          $(this).css("background","#e1ecf1").siblings().css("background","");
       });
    })
    
    // 弹出添加的窗口
    function add(){
    	var isContinue = true;
    	var indexS = 1;
    	var exchangeWay = "";
    	var exchangeAddress = "";
    	var orderNum = "";
    	
    	// 验证数据
    	$("input[name='useWay']").each(
    	    function(){
        		if($(this).get(0).checked){
        			indexS = $(this).attr("index");
        			orderNum = $("#orderNum" + indexS).val();
        			if(!CheckUtil.isInt(orderNum, "+")){
        				layer.msg("请填写正确的排序号");
        				$("#orderNum" + indexS).focus();
        				isContinue = false;
        				return false;
        			}
        			
        			exchangeAddress = $("#exchangeAddress" + indexS).val();
        			exchangeWay = $(this).attr("exchangeWay");
        			if(!CheckUtil.isNotNull(exchangeAddress) && exchangeWay != 20){
        				layer.msg("请填写交易地点");
                        $("#exchangeAddress" + indexS).focus();
                        isContinue = false;
                        return false;
        			}
        		}
    		}
    	)
    	
    	if(!isContinue){
    		return false;
    	}
    	var data = $("#dataFrom");
    	data = getFormData(data);
    	data = JSON.stringify(data);
        $("#pageData").val(data);
    	$.ajax({
            url: "${tradeV1}/tradeBaseGameVsExchangeWay/saveGameVsExchangeWayData",
            type: "post",
            cache: false,
            data: new FormData($("#saveAndUpdateFrom")[0]),
            dataType:"json",
            processData: false,
            contentType: false,
            async:false,
            success: function(data) {
                
                if(data.code == 0){
                    parent.layer.msg("添加成功！");
                } else if(data.code == 1){
                    parent.layer.msg("修改成功！");
                } else if(data.code == "-2"){
                    parent.window.location.href="${sysManagerV1}/";
                } else {
                    parent.layer.msg(data.message);
                }
                if(data.code >= 0){
                    parent.findData();
                }
            }
        });
    }

    function getFormData(form){
        var data = form.serialize();
    	data = decodeURI(data);
    	var arr = data.split('&');
    	var item, key, value;
    	var newData = {}, array = [];
    	for(var i=0;i<arr.length;i++){
    	    item = arr[i].split('=');
    	    key = item[0];
    	    value = item[1];
      	    newData[key] = value;
        	if(key.indexOf('exchangeAddress') >= 0){
        		if(newData["useWay"] == 10){
        	        array.push(newData);
        	    }
      	        newData={};
    	    }
    	}
    	return array;
    }
</script>
</html>